package com.chenfwind.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.chenfwind.common.Constant;
import com.chenfwind.common.ResultVO;
import com.chenfwind.common.StringUtils;
import com.chenfwind.domain.Book;
import com.chenfwind.domain.Owner;
import com.chenfwind.domain.Trajectory;
import com.chenfwind.domain.User;
import com.chenfwind.domain.vo.BookVO;
import com.chenfwind.domain.vo.UserLoginVO;
import com.chenfwind.domain.vo.UserVO;
import com.chenfwind.service.BookService;
import com.chenfwind.service.OwnerService;
import com.chenfwind.service.TrajectoryService;

@Controller
@RequestMapping("/book")
public class BookController {

	@Autowired
	private  BookService bookService;
	
	@Autowired
	private  OwnerService ownerService;
	
	@Autowired
	private TrajectoryService trajectoryService;
	
	@ResponseBody
	@RequestMapping(value = "/newBook", method = RequestMethod.POST)
	public ResultVO<BookVO> newBook(HttpSession session,
			@RequestParam(value = "bookInfoJson", required = false) String bookInfoJson , @RequestParam(value = "userId",required = false) String userId ) throws Exception {
		ResultVO<BookVO> vo = ResultVO.create();
		BookVO bookInfo = JSON.parseObject(bookInfoJson, BookVO.class);
		if (bookInfo == null || userId == null) {
			return ResultVO.create(Constant.CODE_NOPARAM, Constant.CODE_MSG_NOPARAM);
		}
		Book book = BookVO2Book(bookInfo, null);
		book.setId(StringUtils.genUUID());
		// 状态默认为“未启动”
		book.setStatus(0);
		book.setDelStatus(0);
		bookService.newBook(book,userId,bookInfo);
		return ResultVO.create(Constant.CODE_SUCCESS);
	}
	
	@ResponseBody
	@RequestMapping(value = "/changeBookStatus", method = RequestMethod.POST)
	public ResultVO<String> changeBookStatus(HttpSession session,
			@RequestParam(value = "status", required = false) String status , @RequestParam(value = "bookId",required = false) String bookId ) throws Exception {
		ResultVO<String> vo = ResultVO.create();
		if (status == null || bookId == null) {
			return ResultVO.create(Constant.CODE_NOPARAM, Constant.CODE_MSG_NOPARAM);
		}

		Book book = bookService.findBookById(bookId);
		if (StringUtils.isNull(book)) {
			return ResultVO.create(Constant.CODE_ERROR,"找不到ID为" + bookId + "的书籍");
		}
		book.setStatus(Integer.parseInt(status));
		book.setUpdateTime(new Date());
		bookService.updateBook(book);
		vo.setStatus(Constant.CODE_SUCCESS);
		return vo;
	}
	
	public Book BookVO2Book(BookVO bookVO , Book book){
		if (book == null){
			book = new Book();
			book.setUpdateTime(new Date());
		}

		com.chenfwind.utils.BeanUtils.copyProperties(bookVO, book);
		
		if (bookVO.getAuthor() != null) {
			book.setOtherAuthorName(bookVO.getAuthor());
		}
		if (bookVO.getPress() != null){
			book.setOtherPressName(bookVO.getPress());
		}
		if (bookVO.getGategory() != null) {
			book.setGategory(bookVO.getGategory());
		}
		return book;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getAllBookByUser", method = RequestMethod.POST)
	public ResultVO<List<BookVO>> getAllBookByUser(HttpSession session,
			@RequestParam(value = "userId", required = false) String userId){
		ResultVO<List<BookVO>> vo = ResultVO.create();
		if (userId == null ) {
			return ResultVO.create(Constant.CODE_NOPARAM, Constant.CODE_MSG_NOPARAM);
		}
		List<Book> booklist = bookService.getAllBookByUser(userId);
		List<BookVO> bookVOList = new ArrayList<BookVO>();
		BookVO bookVO = null;
		for (Book book : booklist) {
			bookVO = Book2BookVO(book,null);
			Owner owner = ownerService.getOwner(book.getId());
			bookVO.setOwnerId(owner.getUserId());
			bookVOList.add(bookVO);
		}
		vo.setResult(bookVOList);
		vo.reCalcStatus();
		return vo;
	}
	
	public BookVO Book2BookVO(Book book,BookVO bookVO){
		if (bookVO == null) {
			bookVO = new BookVO();
			if (book.getOtherAuthorName() != null) {
				bookVO.setAuthor(book.getOtherAuthorName());
			}
			if (book.getOtherPressName() != null) {
				bookVO.setPress(book.getOtherPressName());
			}
			Owner owner = ownerService.getOwner(book.getId());
			bookVO.setOwnerId(owner.getUserId());
			
		}
		com.chenfwind.utils.BeanUtils.copyProperties(book, bookVO);
		return bookVO;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getOthorBookByL", method = RequestMethod.POST)
	public ResultVO<List<BookVO>> getOthorBookByL(HttpSession session,
			@RequestParam(value = "userInfoJson", required = false) String userInfoJson){
		ResultVO<List<BookVO>> vo = ResultVO.create();
		UserVO userInfo = JSON.parseObject(userInfoJson, UserVO.class);
		if (userInfo == null ) {
			return ResultVO.create(Constant.CODE_NOPARAM, Constant.CODE_MSG_NOPARAM);
		}
		List<Book> bookList = bookService.getOtherBook(userInfo);
		List<BookVO> bookVOList = new ArrayList<BookVO>();
		BookVO bookVO = null;
		for (Book book : bookList) {
			bookVO = Book2BookVO(book,null);
			Owner owner = ownerService.getOwner(book.getId());
			bookVO.setDistance(book.getGategoryId());
			bookVO.setGategoryId(null);
			bookVO.setOwnerId(owner.getUserId());
			bookVOList.add(bookVO);
		}
		vo.setResult(bookVOList);
		vo.reCalcStatus();
		return vo;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/getBookByID", method = RequestMethod.POST)
	public ResultVO<BookVO> getBookByID(HttpSession session,
			@RequestParam(value = "bookId", required = false) String bookId){
		ResultVO<BookVO> vo = ResultVO.create();
		if (bookId == null ) {
			return ResultVO.create(Constant.CODE_NOPARAM, Constant.CODE_MSG_NOPARAM);
		}
		Book book = bookService.findBookById(bookId);
		BookVO bookVO = Book2BookVO(book,null);
		Owner owner = ownerService.getOwner(bookId);
		bookVO.setOwnerId(owner.getUserId());
		vo.setResult(bookVO);
		vo.reCalcStatus();
		return vo;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getAllBook", method = RequestMethod.POST)
	public ResultVO<List<BookVO>> getAllBook(HttpSession session){
		ResultVO<List<BookVO>> vo = ResultVO.create();
		
		List<Book> booklist = bookService.getAllBook();
		List<BookVO> bookVOList = new ArrayList<BookVO>();
		Trajectory track ;
		BookVO bookVO = null;
		for (Book book : booklist) {
			bookVO = Book2BookVO(book,null);
			track = trajectoryService.getTrajectoryByBook(book.getId());
			bookVO.setX(track.getX().toString());
			bookVO.setY(track.getY().toString());
			bookVO.setTrackName(track.getBooktrackname());
			bookVOList.add(bookVO);
		}
		vo.setTotal(bookVOList.size());
		vo.setRows(bookVOList);
		return vo;
	}
	
	@ResponseBody
	@RequestMapping(value = "/deleteBook", method = RequestMethod.POST)
	public ResultVO<List<BookVO>> deleteBook(HttpSession session,@RequestParam(value = "bookId", required = false)String bookId){
		ResultVO<List<BookVO>> vo = ResultVO.create();
		if (bookId == null ) {
			return ResultVO.create(Constant.CODE_NOPARAM, Constant.CODE_MSG_NOPARAM);
		}
		bookService.delBook(bookId);
		List<Book> booklist = bookService.getAllBook();
		List<BookVO> bookVOList = new ArrayList<BookVO>();
		Trajectory track ;
		BookVO bookVO = null;
		for (Book book : booklist) {
			bookVO = Book2BookVO(book,null);
			track = trajectoryService.getTrajectoryByBook(book.getId());
			bookVO.setX(track.getX().toString());
			bookVO.setY(track.getY().toString());
			bookVO.setTrackName(track.getBooktrackname());
			bookVOList.add(bookVO);
		}
		vo.setTotal(bookVOList.size());
		vo.setRows(bookVOList);
		return vo;
	}
	
	@ResponseBody
	@RequestMapping(value = "/searchBook", method = RequestMethod.POST)
	public ResultVO<List<BookVO>> searchBook(HttpSession session,
			@RequestParam(value = "bookName", required = false) String bookName,
			@RequestParam(value = "category", required = false) String category){
		ResultVO<List<BookVO>> vo = ResultVO.create();
		List<Book> bookList = bookService.getBookBy(bookName,category);
		List<BookVO> bookVOList = new ArrayList<BookVO>();
		BookVO bookVO ;
		for (Book book : bookList) {
			bookVO = Book2BookVO(book,null);
			bookVOList.add(bookVO);
		}
		vo.setResult(bookVOList);
		vo.reCalcStatus();
		return vo;
	}
}
